package network;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.HashMap;

public class NetworkCreator {
    BufferedReader br;
    Network network;
    public NetworkCreator(String networkFile) {
        network = new Network();
        FileReader fr = null;
        try {
            fr = new FileReader(networkFile);
            br = new BufferedReader(fr);
            getVertices();
            getEdges();
            br.close();
        } catch (IOException e) {
            System.out.print("Error: while reading" + e);
            System.exit(2);
        }
    }

    public Network getNetwork() {
        return network;
    }

    private void getVertices() throws IOException {
        String line = br.readLine();
        StringTokenizer lineTokenizer=new StringTokenizer(line," ");
        String word = lineTokenizer.nextToken();
        word = lineTokenizer.nextToken();

        int verices = Integer.parseInt(word);
        for(int i=0; i<verices;i++){
            line = br.readLine();
            lineTokenizer=new StringTokenizer(line," ");
            // read first 5 known words
            word=lineTokenizer.nextToken();
            int num = Integer.parseInt(word);
            String label = lineTokenizer.nextToken().toLowerCase();
            double x = Double.parseDouble(lineTokenizer.nextToken());
            double y = Double.parseDouble(lineTokenizer.nextToken());
            double z = Double.parseDouble(lineTokenizer.nextToken());

            HashMap<String, String> params = new HashMap<String, String>();
            // extra params
            while (lineTokenizer.hasMoreTokens()){
                String param=lineTokenizer.nextToken();
                String value=lineTokenizer.nextToken();

                params.put(param, value);
            }

            network.addVertex(new Vertex(num, label, x, y, z, params));
        }
    }

    private void getEdges() throws IOException{
        String line = br.readLine();
        StringTokenizer lineTokenizer;
        String word;
        line = br.readLine();
        while(line != null){

            lineTokenizer=new StringTokenizer(line," ");

            // read first 3 known words
            word=lineTokenizer.nextToken();
            int vertex1 = Integer.parseInt(word);
            word=lineTokenizer.nextToken();
            int vertex2 = Integer.parseInt(word);
            word=lineTokenizer.nextToken();
            int val = Integer.parseInt(word);

            HashMap<String, String> params = new HashMap<String, String>();
            // extra params
            while (lineTokenizer.hasMoreTokens()){
                String param=lineTokenizer.nextToken();
                String value=lineTokenizer.nextToken();

                params.put(param, value);
            }

            network.addEdge(vertex1,  vertex2, val, params);
            line = br.readLine();
        }
    }
}
